package com.smartleanx.module.hrm.dal.mysql.risk;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.smartleanx.framework.mybatis.core.mapper.BaseMapperX;
import com.smartleanx.framework.tenant.core.context.TenantContextHolder;
import com.smartleanx.framework.tenant.core.db.TenantBaseDO;
import com.smartleanx.module.hrm.controller.admin.risk.vo.RiskCancelEmpRiskReqVO;
import com.smartleanx.module.hrm.controller.admin.risk.vo.RiskCancelRiskEmpReqVO;
import com.smartleanx.module.hrm.dal.dataobject.risk.RiskDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 风险 Mapper
 *
 * @author tengcj
 */
@Mapper
public interface RiskMapper extends BaseMapperX<RiskDO> {

    /**
     * 根据风险等级统计风险数量
     *
     * @param level
     * @return
     */
    default Long selectCountByRiskLevel(Integer level) {
        return selectCount(new LambdaQueryWrapperX<RiskDO>()
                .eqIfPresent(RiskDO::getLevel, level)
                .eq(RiskDO::getIgnored, false)
        );
    }

    // @Select("select count(distinct employee_id) from hrm_risk where ignored = false and deleted = 0")
    // Long selectRiskEmpCount();

    /**
     * 根据员工id删除风险
     *
     * @param employeeId
     */
    default void deleteRiskByEmpId(Long employeeId) {
        delete(RiskDO::getEmployeeId, employeeId);
    }

    @DataPermission(enable = false) //是否开启权限
    default List<RiskDO> selectEmpRiskList(Long employeeId, Long deptId, Long riskId, Integer level) {
        return selectList(new LambdaQueryWrapperX<RiskDO>()
                .eqIfPresent(RiskDO::getEmployeeId, employeeId)
                .eqIfPresent(RiskDO::getDeptId, deptId)
                .eqIfPresent(RiskDO::getRiskId, riskId)
                .eqIfPresent(RiskDO::getLevel, level)
                .eq(RiskDO::getIgnored, false)
        );
    }

    // default RiskDO selectEmpRisk(Long employeeId, Long riskId) {
    //     return selectOne(new LambdaQueryWrapperX<RiskDO>()
    //             .eq(employeeId != null, RiskDO::getEmployeeId, employeeId)
    //             .isNull(employeeId == null, RiskDO::getEmployeeId)
    //             .eq(riskId != null, RiskDO::getRiskId, riskId)
    //             .isNull(riskId == null, RiskDO::getRiskId)
    //             .isNull(RiskDO::getRecordId)
    //     );
    // }

    default RiskDO selectEmpRisk(Long employeeId, Long riskId, Long recordId) {
        return selectOne(new LambdaQueryWrapperX<RiskDO>()
                .eq(employeeId != null, RiskDO::getEmployeeId, employeeId)
                .isNull(employeeId == null, RiskDO::getEmployeeId)
                .eq(riskId != null, RiskDO::getRiskId, riskId)
                .isNull(riskId == null, RiskDO::getRiskId)
                .eq(recordId != null, RiskDO::getRecordId, recordId)
                .isNull(recordId == null, RiskDO::getRecordId)
        );
    }

    default List<RiskDO> selectEmpRisk(Set<Long> employeeIds, Long riskId) {
        return selectList(new LambdaQueryWrapperX<RiskDO>()
                .in(RiskDO::getEmployeeId, employeeIds)
                .eqIfPresent(RiskDO::getRiskId, riskId)
        );
    }

    default List<RiskDO> selectRisk(Long riskId, Set<Long> recordIds) {
        return selectList(new LambdaQueryWrapperX<RiskDO>()
                .eq(RiskDO::getRiskId, riskId)
                .in(RiskDO::getRecordId, recordIds)
        );
    }

    // default int deleteRisk(Long employeeId, Long riskType) {
    //     return delete(new LambdaQueryWrapperX<RiskDO>()
    //             .eqIfPresent(RiskDO::getEmployeeId, employeeId)
    //             .eq(RiskDO::getRiskId, riskType)
    //     );
    // }

    default int updateEmpDept(Long employeeId, Long deptId) {
        return update(new LambdaUpdateWrapper<RiskDO>()
                .eq(RiskDO::getEmployeeId, employeeId)
                .set(RiskDO::getDeptId, deptId)
        );
    }

    default int cancelRisk(RiskCancelRiskEmpReqVO reqVO) {
        return update(new LambdaUpdateWrapper<RiskDO>()
                .eq(reqVO.getRiskId() != null, RiskDO::getRiskId, reqVO.getRiskId())
                .in(RiskDO::getEmployeeId, reqVO.getEmployeeIdList())
                .set(RiskDO::getIgnored, true)
        );
    }

    default int cancelRisk(RiskCancelEmpRiskReqVO reqVO) {
        return update(new LambdaUpdateWrapper<RiskDO>()
                .in(RiskDO::getRiskId, reqVO.getRiskIdList())
                .eq(RiskDO::getEmployeeId, reqVO.getEmployeeId())
                .set(RiskDO::getIgnored, true)
        );
    }
}